package com.lcj.nebula.controller;

import com.github.pagehelper.PageInfo;
import com.lcj.common.exception.AochException;
import com.lcj.common.util.Result;
import com.lcj.nebula.controller.form.IdForm;
import com.lcj.nebula.controller.form.OrderIdForm;
import com.lcj.nebula.controller.form.SearchMonitoringForm;
import com.lcj.nebula.controller.vo.OrderMonitoringEntityVo;
import com.lcj.nebula.db.pojo.OrderVoiceTextEntity;
import com.lcj.nebula.service.MonitoringService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/monitoring")
@Tag(name = "MonitoringController", description = "监控服务的Web接口")
@Slf4j
public class MonitoringController {
    @Resource
    private MonitoringService monitoringService;

    @PostMapping(value = "/uploadRecordFile")
    @Operation(summary = "上传代驾录音文件")
    public Result uploadRecordFile(@RequestPart("file") MultipartFile file, @RequestPart("name") String name, @RequestPart(value = "text", required = false) String text) {
        if (file.isEmpty()) {
            throw new AochException("录音文件不能为空");
        }
        monitoringService.monitoring(file, name, text);
        return Result.ok();
    }

    @PostMapping(value = "/insertOrderMonitoring")
    @Operation(summary = "添加订单监控摘要记录")
    public Result insertOrderMonitoring(@RequestBody @Valid OrderIdForm form) {
        int rows = monitoringService.insertOrderMonitoring(form.getOrderId());
        return Result.ok().put("rows", rows);
    }

    @PostMapping("/searchOrderMonitoringByPage")
    public Result searchOrderMonitoringByPage(@RequestBody @Valid SearchMonitoringForm form){
        PageInfo<OrderMonitoringEntityVo> list = monitoringService.searchOrderMonitoringByPage(form);
        return Result.ok().put("result", list);
    }

    @PostMapping("/searchOrderMonitoringDetail")
    public Result searchOrderMonitoringDetail(@RequestBody @Valid IdForm form){
        List<OrderVoiceTextEntity> res = monitoringService.searchOrderMonitoringDetail(form.getId());
        return Result.ok().put("result", res);
    }
}